package com.shengdong.water.code.base

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.shengdong.water.R
import com.shengdong.water.base.UiState
import com.shengdong.water.code.utils.StatusBarUtil
import kotlinx.coroutines.launch

abstract  class BaseActivity<VB : ViewDataBinding>: AppCompatActivity() {
    protected lateinit var binding: VB

    private lateinit var baseBinding: ViewDataBinding



    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 初始化状态栏
        initStatusBar()
        initBaseLayout()
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT//竖屏

        initContentView()
        setupView()
//        initViews()
//        initData()
//        observeData()


        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                handleBackPress()
                finish()
            }
        })

    }
    // 是否使用沉浸式状态栏
    protected open fun useImmersiveStatusBar(): Boolean = true

    // 状态栏是否是黑色字体
    protected open fun statusBarDarkMode(): Boolean = true

    // 状态栏颜色，默认透明
    protected open fun statusBarColor(): Int = Color.TRANSPARENT

    // 是否显示状态栏
    protected open fun showStatusBar(): Boolean = true

    //是否显示标题栏
    protected open fun showTitleBar(): Boolean = true

    private fun initBaseLayout() {
        baseBinding = DataBindingUtil.setContentView(this, R.layout.layout_base_titlbar)
        baseBinding.root.findViewById<TitleBar>(R.id.title_bar).visibility = if (showTitleBar()) View.VISIBLE else View.GONE
        baseBinding.root.findViewById<TitleBar>(R.id.title_bar).setOnTitleBarListener(object : OnTitleBarListener  {
            override fun onLeftClick(titleBar: TitleBar?) {
                super.onLeftClick(titleBar)
                onBackPressedDispatcher.onBackPressed()
            }

            override fun onRightClick(titleBar: TitleBar?) {
                super.onRightClick(titleBar)
                handleTitleBarRightClick()
            }
        })

    }

    open fun initContentView() {
        binding = DataBindingUtil.inflate(
            layoutInflater,
            getLayoutId(),
            findViewById(R.id.fl_content),
            true
        )
        binding.lifecycleOwner = this

    }

    open fun initStatusBar() {
        if (useImmersiveStatusBar()) {
            // 设置沉浸式状态栏
            StatusBarUtil.setImmersiveStatusBar(this)
            // 设置状态栏颜色
            StatusBarUtil.setStatusBarColor(this, statusBarColor())
            // 设置状态栏明暗模式
            StatusBarUtil.setStatusBarDarkMode(this, statusBarDarkMode())
            // 设置状态栏可见性
            StatusBarUtil.setStatusBarVisibility(this, showStatusBar())

            StatusBarUtil.setFitsSystemWindows(this, true)
        }
    }

    // 设置标题（可在子类中调用）
    open fun setTitle(text: String) {
        baseBinding.root.findViewById<TitleBar>(R.id.title_bar).title = text
    }

    open fun setRightText(text: String) {
        baseBinding.root.findViewById<TitleBar>(R.id.title_bar).rightTitle = text
    }



    protected fun addStatusBarPadding(view: View) {
        if (useImmersiveStatusBar()) {
            StatusBarUtil.addStatusBarPadding(view)
        }
    }

//    open fun initDataBinding(){
//        binding = DataBindingUtil.setContentView(this, getLayoutId())
//        binding.lifecycleOwner = this
//    }

    abstract fun getLayoutId(): Int

    open fun setupView() {}
//    open fun initData() {}
//    open fun observeData() {}

    open fun showLoading(loading: String) {
        // 实现加载对话框
    }

    open fun dismissLoading() {
        // 关闭加载对话框
    }

    open fun showToast(message: String) {
        // 显示toast提示
    }

    open fun showError(message: String) {
        // 显示错误信息

    }


    override fun onDestroy() {
        super.onDestroy()
        if (::binding.isInitialized) {
            binding.unbind()
        }
    }


    open fun handleBackPress() {}

    open fun handleTitleBarRightClick() {}
}